/**
 * 
 */
package com.point.business.database.util;

import java.util.List;

import org.apache.log4j.Logger;

import com.point.business.database.managers.InformationManager;
import com.point.business.database.managers.ScheduleManager;
import com.point.business.database.model.InformationND;
import com.point.business.database.model.InformationNO;
import com.point.business.database.model.InformationSL;
import com.point.business.database.model.Schedule;

/**
 * @author Ilija Trajkoski
 * 
 */
public class ScheduleThread extends Thread {
	private static Logger logger = Logger.getLogger(ScheduleThread.class);

	@Override
	public void run() {
		List<Schedule> activeSchedules = ScheduleManager.getInstance()
				.getAllActiveSchedules();
		EmailMessageComposer composer = new EmailMessageComposer();
		for (Schedule schedule : activeSchedules) {
			try {
				if (schedule.getObjectType().equals("SL")) {
					List<InformationSL> result = InformationManager
							.getInstance().searchSL(schedule.getSearchQuery(),schedule.getLastAlertSentDate());
					if (result.size() > 0) {
						composer.composeAndSendMessageSL(result, schedule);
					}
				} else if (schedule.getObjectType().equals("ND")) {
					List<InformationND> result = InformationManager
							.getInstance().searchND(schedule.getSearchQuery(),schedule.getLastAlertSentDate());
					if (result.size() > 0) {
						composer.composeAndSendMessageND(result, schedule);
					}
				} else if (schedule.getObjectType().equals("UOPB")) {
					List<InformationSL> result = InformationManager
							.getInstance().searchSL(schedule.getSearchQuery(),schedule.getLastAlertSentDate());
					if (result.size() > 0) {
						composer.composeAndSendMessageSL(result, schedule);
					}
				} else {
					List<InformationNO> result = InformationManager
							.getInstance().searchNO(schedule.getSearchQuery(),schedule.getLastAlertSentDate());
					if (result.size() > 0) {
						composer.composeAndSendMessageNO(result, schedule);
					}
				}
			} catch (Exception e) {
				logger.error(e);
			}
		}
	}
}
